import toast from '../utils/toast.js'

// 提交表单
export default class ActionForm {
    static task = null;
    static price = null;
    static moneyType = null;
    static priceRate = null;

    // 回调
    static taskIncident = null;
    static priceIncident = null;
    static moneyTypeIncident = null;

    // 任务改变
    taskChange(event, incident) {
        this.task = event.target.value;
        this.taskIncident = incident;
        incident(event.target.value);
    }


    // 价格改变
    priceChange(event, incident, exchangeRate) {
        this.price = event.target.value;
        this.priceIncident = incident;
        if (exchangeRate && this.moneyType && this.price) {
            this.priceRate = exchangeRate.calculate(this.moneyType, this.price, this.setExchange);
        }
        incident(event.target.value);
    }

    // 下拉框改变
    moneyTypeChange(event, incident, exchangeRate) {
        this.moneyType = event.target.value;
        this.moneyTypeIncident = incident;
        if (exchangeRate && this.price && this.moneyType) {
            this.priceRate = exchangeRate.calculate(this.moneyType, this.price, this.setExchange);
        }
        incident(event.target.value);
    }

    addTask() {
        if (this.validateFrom()) {
            const obj = {
                timestamp: new Date().getTime(),
                isComplete: false,
                taskName: this.task,
                taskPrice: this.price,
                CNY: this.priceRate.CNY,
                RUB: this.priceRate.RUB,
                USD: this.priceRate.USD
            }
            //制空
            this.task = null;
            this.price = null;
            this.moneyType = null;
            this.taskIncident('');
            this.priceIncident('');
            this.moneyTypeIncident('');
            return obj;
        }
    }

    validateFrom() {
        if (!this.task) {
            toast.warning('请输入任务', 2000);
            return false;
        } else if (!this.price) {
            toast.warning('请输入价格', 2000);
            return false;
        } else if (!this.pric && isNaN(parseFloat(this.price))) {
            toast.warning('请输入数值类型', 2000);
            return false;
        } else if (!this.moneyType) {
            toast.warning('请选择货币类型', 2000);
            return false;
        }else if(!this.priceRate){
            toast.warning('请刷新试试', 2000);
            return false;
        }
        return true
    }


}